package Level;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import org.jbox2d.dynamics.World;
import org.mt4j.MTApplication;
import org.mt4j.components.MTComponent;
import org.mt4j.util.math.Vector3D;

import processing.core.PApplet;

import GameObjects.GameBrick;

public class Maploader {
	
	public static short loadMap(String fileName, MTApplication _app, World _world, MTComponent _physicsContainer, float scale) throws IOException{
		BufferedReader input = new BufferedReader(new FileReader("level\\"+fileName));
		short brickcount = 0;
		
		String line = "";
		
		int spalte = 0;
		while((line = input.readLine()) != null){
			int zeile = 0;
			String [] values = line.split("\\.");
			for(String brickstrength : values){
				if(!brickstrength.equals("0")){
					GameBrick newBrick = new GameBrick(new Vector3D((_app.getWidth()/2)+((zeile*16)+8), (spalte*64)+32), 16, 64, _app, _world, scale,  brickstrength);
					_physicsContainer.addChild(newBrick);
				
					//spiegelung
					GameBrick newMirrorBrick = new GameBrick(new Vector3D((_app.getWidth()/2)-((zeile*16)+8), (spalte*64)+32), 16, 64, _app, _world, scale,  brickstrength);
					_physicsContainer.addChild(newMirrorBrick);
					
					brickcount+=2;
				}
				zeile++;
			}
			spalte++;
		}
		System.out.println("Fertig!");
		return brickcount;
	}
	
}
